/**
 * 
 */
package com.cqcis.uip.base.logadaptor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Appender;

/**
 * 日志配置
 * 
 * @author huangmy
 * @date 2008-2-15
 */
public class LogConfiguration implements Serializable {
	private static final long serialVersionUID = 1L;
	// ----- 日志组件类型 ----- //
	public final static int LOGAPPENDER_TYPE_FILE = 0; // 文件
	public final static int LOGAPPENDER_TYPE_LOGOBJECT = 1; // 日志对象, 每条日志为一个对象
	private final static String LOGLEVEL_DEFAULT = "DEFAULT";
	private String dirname; /* 日志目录名称 */
	private String level = LOGLEVEL_DEFAULT; /* 日志级别 */
	private String conversionPattern; // 默认为空
	private int logAppenderType = LOGAPPENDER_TYPE_FILE; // 日志组件类型	
	private transient Appender logObjectAppender; // 日志组件 	
    private transient Attributes<Object> attributes = new Attributes<Object>();
    private transient List<LogConfiguration> relateConfigs = new ArrayList<LogConfiguration>();
    private boolean destroyed; // 销毁
    private boolean withTempleteInSameDir = false;// 默认与模板的上层目录平级
        	
	public LogConfiguration() {		
	}
	
	public LogConfiguration(String dirname, String level) {
		this.dirname = dirname;
		this.level = level;
	}
	
	public LogConfiguration(String dirname, String level, String conversionPattern) {
		this.dirname = dirname;
		this.level = level;
		this.conversionPattern = conversionPattern;
	}
		
	/**
	 * 销毁
	 * 
	 */
	public void destroy() {
		destroyed = true;
		
		Appender appender = getLogObjectAppender();
		if (appender != null) {
			appender.close();
		}
		
		Collection<LogConfiguration> relas = getAllRelationConfigs();
		if (relas != null) {
			for (LogConfiguration rela : relas) {
				appender = rela.getLogObjectAppender();
				if (appender != null) {
					appender.close();
				}
			}
		}
	}
	
	@Override
	protected void finalize() throws Throwable {
		try {
			destroy();
		} catch (Throwable t) {			
		}
		super.finalize();
	}
	
	public void addRelationConfig(LogConfiguration logConfig) {
		this.relateConfigs.add(logConfig);
	}
	
	public void addRelationConfigs(Collection<LogConfiguration> logConfigs) {
		this.relateConfigs.addAll(logConfigs);
	}
	
	public void removeRelationConfig(LogConfiguration logConfig) {
		this.relateConfigs.remove(logConfig);
	}
	
	public Collection<LogConfiguration> getAllRelationConfigs() {
		return this.relateConfigs;
	}
	
	public String getDirname() {
		return dirname;
	}

	public void setDirname(String dirname) {
		this.dirname = dirname;
	}

	public String getLevel() {
		if (level == null) {
			return LOGLEVEL_DEFAULT;
		}
		return level;
	}

	public void setLevel(String level) {
		if (level == null) {
			level = LOGLEVEL_DEFAULT;
		}
		this.level = level;
	}
	
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("dir: " ).append(dirname)
		      .append("; level: ")
		      .append(level)
		      .append("; conversionPattern: ")
		      .append(conversionPattern)
		      .append("; logAppenderType: ")
		      .append(logAppenderType)
		      ;
		
		return buffer.toString();
	}

	public String getConversionPattern() {
		return conversionPattern;
	}

	public void setConversionPattern(String conversionPattern) {
		this.conversionPattern = conversionPattern;
	}

	public Attributes<Object> getAttributes() {
		return attributes;
	}

	public int getLogAppenderType() {
		return logAppenderType;
	}

	public void setLogAppenderType(int logAppenderType) {
		this.logAppenderType = logAppenderType;
	}

	public Appender getLogObjectAppender() {
		return logObjectAppender;
	}

	public void setLogObjectAppender(Appender logObjectAppender) {
		this.logObjectAppender = logObjectAppender;
	}

	public boolean isDestroyed() {
		return destroyed;
	}

	public boolean isWithTempleteInSameDir() {
		return withTempleteInSameDir;
	}

	public void setWithTempleteInSameDir(boolean withTempleteInSameDir) {
		this.withTempleteInSameDir = withTempleteInSameDir;
	}
}
